package com.fenyin.samples.study.parttern.struct.adapter.example1;  

import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *<p>EnumerationIterator: 适配器类</p>
 *<p>Iterator:适配器目标接口<p>
 *<p>Enumeration:适配器源对象<p>
 *@Title: 对象适配器模式
 *@Author:fengzhichao  
 *@Since:2012-9-29  
 *@Version:1.1.0
 */
public class EnumerationIterator<T> implements Iterator<T>{
	
	private static final Log log = LogFactory.getLog(EnumerationIterator.class);

	/**适配器源对象*/
	private Enumeration<T> enumeration;
	
	public EnumerationIterator(Enumeration<T> enumeration){
		this.enumeration = enumeration;
	}
	
	public boolean hasNext() {
		return enumeration.hasMoreElements();
	}

	public T next() {
		return enumeration.nextElement();
	}

	public void remove() throws UnsupportedOperationException{
		throw new UnsupportedOperationException("Not supported");
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args){
		Enumeration enumeration = new StringTokenizer("name=fzc#age=18","#");
		Iterator<String> iterator = new EnumerationIterator<String>(enumeration);
		while(iterator.hasNext()){
			log.info("数据元素:"+iterator.next());
		}
	}
}
